
package com.shop.cloud.mall.api.controller;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shop.cloud.common.core.util.R;
import com.shop.cloud.mall.api.service.CouponInfoService;
import com.shop.cloud.mall.api.service.GoodsSpuService;
import com.shop.cloud.mall.common.entity.CouponGoods;
import com.shop.cloud.mall.common.entity.CouponInfo;
import com.shop.cloud.mall.common.entity.CouponUser;
import com.shop.cloud.common.portal.util.ThirdSessionHolder;
import com.shop.cloud.mall.common.entity.GoodsSpu;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Operation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * 电子券
 *
 * @author
 * @date 2019-12-14 11:30:58
 */
@Slf4j
@RestController
@AllArgsConstructor
@RequestMapping("/couponinfo")
@Tag(description = "couponinfo", name = "电子券API")
public class CouponInfoController {

    private final CouponInfoService couponInfoService;
	private final GoodsSpuService goodsSpuService;

	/**
	 * 分页查询
	 * @param page 分页对象
	 * @param couponInfo 电子券
	 * @return
	 */
	@Operation(summary = "分页查询")
    @GetMapping("/page")
    public R getPage(Page page, CouponInfo couponInfo, CouponGoods cuponGoods) {
		CouponUser couponUser = new CouponUser();
		String userId = ThirdSessionHolder.getUserId();
		if(StrUtil.isNotBlank(userId)){
			couponUser.setUserId(userId);
		}
		if(StrUtil.isNotBlank(cuponGoods.getSpuId())){
			GoodsSpu goodsSpu = goodsSpuService.getById(cuponGoods.getSpuId());
			if(goodsSpu != null){
				couponInfo.setShopId(goodsSpu.getShopId());
			}
		}
        return R.ok(couponInfoService.page2(page, couponInfo, cuponGoods, couponUser));
    }

    /**
     * 通过id查询电子券
     * @param id
     * @return R
     */
	@Operation(summary = "通过id查询电子券")
    @GetMapping("/{id}")
    public R getById(@PathVariable("id") String id) {
        return R.ok(couponInfoService.getById2(id));
    }

}
